package com.example.androidworklhz.act

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.androidworklhz.R

class LoginActivity_XXT : AppCompatActivity() {
    private lateinit var edtPhone: EditText
    private lateinit var edtPassword: EditText

    // 如果启动一个Activity，并且需要该Activity回传参数，需要手动注册启动器（ActivityResultLauncher）
    // 注册ActivityResultLauncher时，写明获取回传参数的逻辑
    private var registerLauncher =
        registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                // 两个Activity中的Key需要保持一致
                // SpannableString String
                val phone = result.data?.extras?.getString("phone")
                val password = result.data?.getStringExtra("password")
                edtPhone.setText(phone)
                edtPassword.setText(password)
            }
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        this.enableEdgeToEdge()
        setContentView(R.layout.activity_login_xxt)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v: View, insets: WindowInsetsCompat ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        edtPhone = findViewById(R.id.edtPhone)
        edtPassword = findViewById(R.id.edtPassword)

        val cbAgree = findViewById<CheckBox>(R.id.cbAgree)
        // 设置部分文字显示不同颜色
        val ss = SpannableString("我已阅读并同意学习通《隐私政策》和《用户协议》")
        ss.setSpan(
            ForegroundColorSpan(Color.parseColor("#0E8BAF")),
            10,
            16,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        ss.setSpan(
            ForegroundColorSpan(Color.parseColor("#0E8BAF")),
            17,
            23,
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
        )
        cbAgree.text = ss

        // 找到注册新用户的TextView，并添加点击事件
        findViewById<TextView>(R.id.tvRegister).setOnClickListener {
            // 跳转页面，从登录-注册
            // 通过ActivityResultLauncher启动
            val intent = Intent(this, RegisterActivity::class.java)
            intent.putExtra("params1","Hello")
            intent.putExtra("params2","Kotlin")
            registerLauncher.launch(intent)
        }

        // 找到登录按钮，并添加点击事件
        findViewById<Button>(R.id.button).setOnClickListener {
            // 跳转到TabHostActivity
            val intent = Intent(this, MainActivity_XXT::class.java)
            startActivity(intent)
        }
    }

}